<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small" label-width="100">
      <el-form-item label="活动名称：">
        <el-input v-model="listQuery.keyword" class="input-width" placeholder="活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button type="primary" size="small" class="btn-add" @click="handleAdd">新增</el-button>
        <el-button size="small" class="btn-add" @click="handleShowSessionList">秒杀时间段列表</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="flashTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="活动标题" min-width="200" align="center">
          <template #default="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="活动状态" width="140" align="center">
          <template #default="scope">{{ formatActiveStatus(scope.row) }}</template>
        </el-table-column>
        <el-table-column label="开始时间" width="140" align="center">
          <template #default="scope">{{ formatDate(scope.row.startDate) }}</template>
        </el-table-column>
        <el-table-column label="结束时间" width="140" align="center">
          <template #default="scope">{{ formatDate(scope.row.endDate) }}</template>
        </el-table-column>
        <el-table-column label="上线/下线" width="100" align="center">
          <template #default="scope">
            <el-switch @change="handleStatusChange(scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleSelectSession(scope.row)"> 设置商品 </el-button>
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="添加活动" v-model="dialogVisible" width="40%">
      <el-form :model="flashPromotion" ref="flashPromotionForm" label-width="auto" size="small">
        <el-form-item label="活动标题：">
          <el-input v-model="flashPromotion.title"></el-input>
        </el-form-item>
        <el-form-item label="活动时间：">
          <el-date-picker v-model="dateRange" type="daterange" start-placeholder="开始日期" end-placeholder="结束时间"> </el-date-picker>
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="flashPromotion.status">
            <el-radio :value="1">上线</el-radio>
            <el-radio :value="0">下线</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, updateStatus, deleteFlash, createFlash, updateFlash } from '@/api/flash'
import { computed, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { Flash, FlashPageDto } from '@/models/flash'
import { formatDate } from '@/utils/format'

defineOptions({ name: 'flashPromotionList' })
const router = useRouter()
const listQuery = ref<FlashPageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<Flash[]>([])
const total = ref(0)
const listLoading = ref(false)
const dialogVisible = ref(false)
const flashPromotion = ref<Flash>({})
const dateRange = computed({
  get() {
    const { startDate, endDate } = flashPromotion.value
    return [startDate, endDate]
  },
  set(val = []) {
    flashPromotion.value.startDate = val[0]
    flashPromotion.value.endDate = val[1]
  },
})

getList()

const formatActiveStatus = (row: Flash) =>
  Date.now() > Number(row.endDate) ? '活动已结束' : Date.now() < Number(row.startDate) ? '活动未开始' : '活动进行中'

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}

function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}

function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}

function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}

function handleAdd() {
  dialogVisible.value = true
  flashPromotion.value = {}
}

function handleShowSessionList() {
  router.push({ path: '/sms/flash/session' })
}

async function handleStatusChange(row: Flash) {
  await updateStatus({ status: row.status as number, id: row.id as number })
  ElMessage.success('修改成功')
}

async function handleDelete(row: Flash) {
  await ElMessageBox.confirm('是否要删除该活动?', '提示', { type: 'warning' })
  await deleteFlash(row.id as number)
  ElMessage.success('删除成功')
  getList()
}

function handleUpdate(row: Flash) {
  dialogVisible.value = true
  flashPromotion.value = row
}

async function handleDialogConfirm() {
  if (flashPromotion.value.id) {
    await updateFlash(flashPromotion.value.id, flashPromotion.value)
    ElMessage.success('添加成功！')
  } else {
    await createFlash(flashPromotion.value)
    ElMessage.success('添加成功！')
  }
  dialogVisible.value = false
  getList()
}

function handleSelectSession(row: Flash) {
  router.push({ path: '/sms/flash/selectSession', query: { flashPromotionId: row.id } })
}

async function getList() {
  listLoading.value = true
  try {
    const response = await fetchList(listQuery.value)
    list.value = response.data.data.list
    total.value = response.data.data.total
  } finally {
    listLoading.value = false
  }
}
</script>
<style></style>
